import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from "@/store"




const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function (location) {
  return originalPush.call(this, location).catch(err => { })
};

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: Home
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/User.vue'),
    redirect: '/user/cart',
    children: [
      {
        path: 'cart',
        name: 'cart',
        component: () => import('../components/user/Cart.vue')
      }
    ]
    //独享守卫   只有在访问该路由时才触发钩子函数
    // beforeEnter: (to, from, next) => {
    //   let token = sessionStorage.getItem("x-auth-token");
    //   if (token) {
    //     next();
    //   } else {
    //     store.dispatch("showToast/asyncchangeIsShowToast", {
    //       isshow: true,
    //       msg: "请先登录再尝试",
    //       type: "danger"
    //     })
    //   }
    // }
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import('../views/Goods.vue')
  },
  {
    path: '/free',
    name: 'free',
    component: () => import('../views/Free.vue')
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/Order.vue')
  },
  {
    path: '/detalis',
    name: 'detalis',
    component: () => import('../views/Detali.vue')
  }
  ,
  {
    path: '*',
    name: 'error',
    component: () => import('../components/Error.vue')
  }

]



const router = new VueRouter({
  mode: "history",
  routes
})

//全局守卫，在进入页面之前执行
// router.beforeEach((to, from, next) => {
//   if (to.path == '/user') {
//     let token = sessionStorage.getItem("x-auth-token");
//     if (token) {
//       next();
//     } else {
//       store.dispatch("showToast/asyncchangeIsShowToast", {
//       })
//     }
//   } else {
//     next()
//   }
// })

export default router
